<?php
class Admin_TopupController extends Zend_Controller_Action{

	public function init(){
		parent::init();
	}

	protected function buildOrders($date) {
		$orderMapper = new User_Model_OrderMapper();
		$orders = $orderMapper->getOrdersByDate($date);
		$userMapper = new Front_Model_UserMapper();

		if (isset($orders)) {
			$rowdata = array();
			$oids = array();

			foreach ($orders as $order) {
				if ($order->_status != 3) {
					$row = array();
					$oids[] = $order->_oid;
					$menu = $orderMapper->getMenu($order->_oid);
					$user = new Front_Model_User();
					$hasUser = $userMapper->find($order->_uid, $user);
					if (isset($menu) && isset($hasUser)) {
						$row['userfullname'] = $user->_fullname;
						$row['menuname'] = $menu->_menuname;
						//$row['menuimage'] = $menu->_menuimage;
						$typeInfo = $orderMapper->getType($order->_oid);
						$type = '';
						if (isset($typeInfo)) {
							$type = $typeInfo->_typename;
						}
						$row['type'] = $type;
						$row['quantity'] = $order->_quantity;
						$checked = '';
						if ($order->_status == 0) {
							//$checked = 'checked';
						} else if ($order->_status == 1) {
							//$checked = '';
						}
						$status = "<input type='checkbox' ".$checked."
							name='".$order->_oid."'/>";
						$row['status'] = $status;
						$rowdata[] = $row;
					}
				}
			}
			$this->view->oids =  implode(',',$oids);
			$this->view->rowdata  = $rowdata;
		}
	}

	public function indexAction() {
		$auth = Zend_Auth::getInstance();
		$indentify = $auth->getIdentity();
		$date = $this->getRequest()->getParam('date');
		$formDate = new Admin_Form_DateSelect();
		$this->view->formdate = $formDate;
		$formDate->setMethod('get');
		if (isset($indentify) && isset($date)) {
			$formDate->populateData($date);
			$uid = $indentify->uid;
			if (isset($uid)) {

				if ($this->getRequest()->isPost()) {
					$oids = $this->getRequest()->getParam('oids');
					$oids = explode(',', $oids);
					if (is_array($oids)) {
						$orderMapper = new User_Model_OrderMapper();
						foreach ($oids as $oid) {
							$orderFinish = false;
							if (isset($_POST[$oid])) {
								$orderFinish = true;
							}
							if ($orderFinish) {
								$this->updateStatus($oid, 3, $orderMapper);
							}
						}
					}
				}
				$this->buildOrders($date);
			}
		}
	}

	protected function updateStatus($oid, $status, User_Model_OrderMapper $orderMapper) {
		$orderInfo = new User_Model_Order();
		if ($orderMapper->find($oid, $orderInfo) ) {
			$orderInfo->_status = $status;
			$orderMapper->save($orderInfo);
		}
	}
}